<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php

class PasswdModifyForm extends CFormModel
{
    public $oldPasswd;
    public $passwd;
    public $passwdConfirm;
    public function rules()
    {
        return array(
        array('oldPasswd,passwd,passwdConfirm', 'required'),
        array('passwd', 'length', 'min'=>5, 'max'=>20),
        array('passwdConfirm', 'compare', 'compareAttribute' => 'passwd'),
        array('oldPasswd', 'validOldPasswd'),
        array('passwd', 'checkPassword'),
        );
    }
    public function attributeLabels()
    {
        return array(
            'oldPasswd'     => '当前密码',
            'passwd'        => '新密码',
            'passwdConfirm' => '确认新密码', 
        );
    }
    
    public function validOldPasswd(){
        if($this->hasErrors()==false){
            $id = Yii::app()->session["user"]["id"];
            $user = User::model()->findByPk($id);
            $dbPasswd = $user["user_pass"];
            if($dbPasswd==CSecurity::signPasswd($this->oldPasswd, $user["salt"])){
                return true;
            }else{
                $this->addError('oldPasswd','原密码输入错误');
            }
        }
        return false;
    }

    
    public function checkPassword(){
        if(!$this->hasErrors())
        {
            $strArr = explode(" ", $this->passwd);
            if(count($strArr)!=1){
                $this->addError('passwd','密码中不能有空格');
                return;
            }
        }
    }

    public function save(){
        if($this->validate()){
            $id = Yii::app()->session["user"]["id"];
            $user = User::model()->findByPk($id);
            $user["user_pass"] = CSecurity::signPasswd($this->passwd, $user["salt"]);
            $user->save();
            return true;
        }
        return false;
    }
}